<?php
	namespace Common\Logic;
	use Common\Model\UserRechargeLogsModel;
	class UserRechargeLogsLogic extends UserRechargeLogsModel{
		/*
		* Function:		getUserRechargeLogsList
		* Description:	用户充值消费列表
		* @param:		$page string 起始页
						$filter array 过滤数组
						$pageSize string 每页显示条数
		*/
		public function getUserRechargeLogsList($page=1,$filter = '',$pageSize=20){
			$conditon = $this->getUserRechargeLogsFilter($filter);
			return $this->where($conditon)->order('id desc')->limit(($page-1)*$pageSize,$pageSize)->select();
		}
		
		/*
		* Function:get:	getUserRechargeLogsFilter
		* Description:	用户充值消费列表搜索过滤项
		* @param:		$filter array 过滤数组
		*/
		public function getUserRechargeLogsFilter($filter = ''){
			$conditon = array();
			if(!empty($filter['user_id'])){
				$conditon['user_id'] = $filter['user_id'];
			}
         	if(!empty($filter['type'])){
				$conditon['type'] = $filter['type'];
			}
			if(!empty($filter['username'])){
				$wh = array();
				$wh['username'] = array('like','%'.$filter['username'].'%');
				$sql = D('User')->field('id')->where($wh)->select(false);
				$conditon['user_id'] = array('exp','in('.$sql.')');
			}
			if(!empty($filter['flag'])){
			    $conditon['status'] = $filter['status'];
			}
			return $conditon;
		}
		
		/*
		* Function:get:	getUserRechargeLogsCount
		* Description:	用户充值消费列表总数
		* @param:		$filter array 过滤数组
		*/
		public function getUserRechargeLogsCount($filter = ''){
			$conditon = $this->getUserRechargeLogsFilter($filter);
			return $this->where($conditon)->count();
		}
		
		/*
		* Function:		addUserRechargeLogs
		* Description:	增加用户充值消费
		* @param:		$data array 增加的数组
		*/
		public function addUserRechargeLogs($data){
			return $this->add($data);
		}
		
		/*
		* Function:		getUserRechargeLogsInfo
		* Description:	获取用户充值消费信息 by row
		* @param:		$id int ID
		*/
		public function getUserRechargeLogsInfo($id){
			return $this->where(array('id'=>$id))->find();
		}

        /*
		* Function:		getUserRechargeLogsInfo
		* Description:	获取用户充值消费信息 by row
		* @param:		$id int ID
		*/
		public function getUserRechargeLogsByOrderId($order_id){
			return $this->where(array('order_id'=>$order_id))->find();
		}
		
		/*
		* Function:		updateUserRechargeLogs
		* Description:	更新用户充值消费
		* @param:		$data array 更新的数组
		*/
		public function updateUserRechargeLogs($data){
			return $this->save($data);
		}
		
		/*
		* Function:		deleteUserRechargeLogs
		* Description:	删除用户充值消费
		* @param:		$id int ID
		*/
		public function deleteUserRechargeLogs($id){
			return $this->where(array('id'=>$id))->delete();
		}

        /*
		* Function: generateOrderId
		* Description:生成唯一订单号
		* @param:$supplierCode string 商家编码orcode
		*/
		public function generateOrderId($code = ''){
			$order_id = $code.date('YmdHis',time()).rand(10000,99999);
			if($this->getUserRechargeLogsByOrderId($order_id)){
				return $this->generateOrderId($code);
			}
			return $order_id;
		}

        /*
		* Function:		addUserReacharge
		* Description:	用户增加充值
		*/
		public function addUserReacharge($data = []){
			$return = [];
            $data['type'] = 1;
            $data['title'] = '账户充值';
			$data['order_id'] = $this->generateOrderId('RC');
			$ret = $this->addUserRechargeLogs($data);
			if(empty($ret)){
				$this->rollback();
				return ['code'=>0,'errorMsg'=>'生成充值订单失败'];
			}
			return ['code'=>1,'errorMsg'=>'success','order_id'=>$data['order_id'],'price'=>$data['balance']];
		}
	}
?>